Click here to Skip to main content
15,899,314 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
in c# 2008, I'm trying to copy a file to a destination path (for example \newserver\destinationFolder), that can be in another domain or using a different username/password than the current user. How can I specify these new network credentials before doing the File.Copy(...) ?


pls guide me a way to copy file onto remote server of different domain with remote server's user account.

Thanks inadvance...
Posted
Updated 23-Jun-11 0:19am
v2

I think this will help you

try this link :

http://stackoverflow.com/questions/6307055/copy-file-to-remote-computer-with-c-script-and-nant-a-specified-logon-session[^]

Or sample code:

Imports System 
Imports System.Runtime.InteropServices 
Imports System.Security.Principal 
Imports System.Security.Permissions 
Public Class Form1 
    <DllImport("advapi32.DLL", SetLastError:=True)> _ 
    Public Shared Function LogonUser(ByVal lpszUsername As String, ByVal lpszDomain As String, _ 
        ByVal lpszPassword As String, ByVal dwLogonType As Integer, ByVal dwLogonProvider As Integer, _ 
        ByRef phToken As IntPtr) As Integer 
    End Function 
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 
        Dim admin_token As IntPtr 
        Dim wid_current As WindowsIdentity = WindowsIdentity.GetCurrent() 
        Dim wid_admin As WindowsIdentity = Nothing 
        Dim wic As WindowsImpersonationContext = Nothing 
        Try 
            MessageBox.Show("Copying file...") 
            If LogonUser("Local Admin name", "Local computer name", "pwd", 9, 0, admin_token) <> 0 Then 
                wid_admin = New WindowsIdentity(admin_token) 
                wic = wid_admin.Impersonate() 
                System.IO.File.Copy("C:\right.bmp", "\\157.60.113.28\testnew\right.bmp", True) 
                MessageBox.Show("Copy succeeded") 
            Else 
                MessageBox.Show("Copy Failed") 
            End If 
        Catch se As System.Exception 
            Dim ret As Integer = Marshal.GetLastWin32Error() 
            MessageBox.Show(ret.ToString(), "Error code: " + ret.ToString()) 
            MessageBox.Show(se.Message) 
        Finally 
            If wic IsNot Nothing Then 
                wic.Undo() 
            End If 
        End Try 
    End Sub 
End Class
 
Share this answer
 
Comments
surender.m 22-Jun-11 5:23am    
Thanks irfan for the reply..

Here if i use LogonUser(), it will use local system's user account..

In my case if i know domain,username and password of the remote computer i should be able to access network share and able to transfer files.
[no name] 22-Jun-11 5:27am    
Please check this:

http://stackoverflow.com/questions/6307055/copy-file-to-remote-computer-with-c-script-and-nant-a-specified-logon-session
surender.m 22-Jun-11 6:23am    
There is no domain involved in the example..
If i know Remote server's DOMAIN,USERNAME,PASSWORD then i should be able to copy my file onto remote server.
Sistemas Edificasa 7-Sep-16 11:11am    
Excellent Solution Thanks a lot
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900